<%--
  Created by IntelliJ IDEA.
  User: kuankuan
  Date: 2023/3/8
  Time: 16:56
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
    <title>Cart</title>
    <link rel="stylesheet" href="/css/bootstrap.min.css">
    <link rel="stylesheet" href="/css/bootstrap-theme.min.css">
    <script src="/js/jquery-1.12.4.min.js"></script>
    <script src="/js/bootstrap.min.js"></script>
    <style>
        body {
            background: none repeat scroll 0 0 #D8EDFC;
            margin: 0;
            padding: 0;
        }
    </style>
</head>
<body>
<h3>我的购物车</h3>
<table class="table">
    <c:forEach var="item" items="${requestScope.cartItems}">
        <tr>
            <td><img src="${item.food.food_image}" style="width: 100px;height: 100px;"></td>
            <td><div>${item.food.food_name}</div></td>
            <td><div>￥ ${item.food.food_price}</div></td>
            <td><div>${item.cnt}</div></td>
            <td>
                <c:choose>
                    <c:when test="${item.cnt > 0}">
                        <a class="cnt" onclick="add1(${item.food.food_id}, ${item.table_id})">加菜</ a>
                        <a class="cnt" onclick="sub(${item.food.food_id}, ${item.table_id})">减菜</ a>
                    </c:when>
                    <c:when test="${item.cnt == 0}">
                        <a class="cnt" onclick="add1(${item.food.food_id}, ${item.table_id})">加菜</ a>
                    </c:when>
                </c:choose>
            </td>
        </tr>
    </c:forEach>
    <tr><td colspan="4" align="center"><div>总价格: ${requestScope.totalPrice}</div></td></tr>
    <tr>
        <td colspan="5" align="right">
            <button onclick="dingdan1(${requestScope.totalPrice})">提交</button>
        </td>
    </tr>
</table>

</body>

<script>
    var total = 0;
    var foods = []


    function add1(food_id, table_id){
        total++;
        showCnt();
        fetch("/cart?action=add&table="+table_id+"&food="+food_id, {
            method: "put"
        }).then(response=>response.text()).then(data=>{
            alert(data);
            location.reload()
        })
    }

    function sub(food_id, table_id) {
        total--;
        showCnt();
        fetch("/cart?action=sub&table="+table_id+"&food="+food_id, {
            method: "put"
        }).then(response=>response.text()).then(data=>{
            alert(data);
            location.reload()
        })
    }

    function showCnt() {
        $("#cnt").text(total)
    }



    function dingdan1(t) {
        open("/dingdan?action=aaa&totalprice="+t+"&user_id="+${requestScope.user_id},"_self")


    }
</script>
</html>
